package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.modles.CollectSingleGood;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 用户头像适配器
 * Created by lanouhn on 16/9/16.
 */
public class UserIconAdapter extends RecyclerView.Adapter<UserIconAdapter.iconViewHolder> {

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    private Context context;
    private List<CollectSingleGood.DataBean.LikesListBean> likesListBeen = new ArrayList<>();

    public UserIconAdapter(Context context, List<CollectSingleGood.DataBean.LikesListBean> likesListBeen) {
        this.context = context;
        this.likesListBeen = likesListBeen;
    }

    @Override
    public iconViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.view_icon_like_users,null);
        iconViewHolder holder=new iconViewHolder(view);

        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();
        return holder;
    }

    @Override
    public void onBindViewHolder(iconViewHolder holder, final int position) {
        Picasso.with(context).load(likesListBeen.get(position).getAvatar()).into(holder.iv_icon);
        holder.iv_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, OriginalAuthorActivity.class);
                String author_ID =likesListBeen.get(position).getUser_id();
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 8;
    }

    public class iconViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView iv_icon;

        public iconViewHolder(View itemView) {
            super(itemView);
            iv_icon = (CircleImageView) itemView.findViewById(R.id.iv_icon_likes_user);
        }
    }
}
